var idsLoading=1;
function openSession(type){
	if (type=='login'){
		var ventana	=new winPopup('Iniciar Sesion...');
		ventana.ajax = _SYSTEM+"?frm=autenticacion&class=login&type=show";
		ventana.width="350px";
		ventana.draw();
	}
	else if (type=='logout'){
		var ventana	=new winPopup('Esta Seguro...');
                ventana.ajax = _SYSTEM+"?frm=autenticacion&class=logout&type=show";
		ventana.width="250px";
		ventana.height="120px";
		ventana.draw();
	}
}
function login(Object){
	//Object.disabled=true;
	while (true){
		if (Object.tagName=="body") return;
		if (Object.className=="divPopupDialog"){break;}
		Object = Object.parentNode;
	}
	//alert (Object.id);
	var error = ""
	var ajax = new Ajax(_SYSTEM);
	if ($$('txtuser').value=="") error+=" * Ingrese Usuario";$$('txtuser').focus();
	if (error!=""){
		$$('eLogin').innerHTML="Se produjeron los Siguientes Errores<br>"+error;
		return;
	}
        ajax.add("frm","autenticacion");
        ajax.add("class","login");
	ajax.add("type","validate");
	ajax.add("user",$$('txtuser').value);
	ajax.add("pass",$$('txtpassword').value);
	ajax.Sincrono=true
	ajax.Funcion = loadMenu;
	//var ids = loading(document.getElementById('cargar'));
	var ids = loading(Object);
	ajax.anexo = ids;
	ajax.anexo1 = Object.id;
	ajax.execute();
}

function loadMenu(rq, ids, idobject){
	//alert (rq.readyState);
	//var ids = loading();
	//alert (ids);
	if (rq.readyState==4){
		if(rq.status==200) {
                    var response = rq.responseText;
                    //alert (response);
                    if (response=="nologin"){
                        $$('eLogin').innerHTML="*Usuario o Password erroneos";
                    }
                    else if (response=="404"){
                        $$('eLogin').innerHTML="*Error en el Servidor";
                    }
                    else if (response==""){
                        $$('eLogin').innerHTML="*Servidor no devuelve datos";
                    }
                    else{
                        alert (response);
                        var variables = response.split("%");
                        if ($$('divUsuario')!=null){
                                $$('divUsuario').innerHTML = variables[0];
                        }
                        if ($$('divLog').title=='logout'){
                                $$('divLog').title='login';
                                $$('divLog').innerHTML = "Login(Iniciar Sesion)";
                        }
                        else if ($$('divLog').title=='login'){
                                $$('divLog').title='logout';
                                $$('divLog').innerHTML = "Logout(Cerrar Sesion)";
                        }
                        var ids = loading();
                        closePopup($$(idobject));
                        menuItems = eval(variables[1]);
                        _load_menu();
                        showMenu('divMenu');
                    }
                    unloading(ids);
                    //alert (response);
		}
	}
	//if (rq.)
}
function logout(Object){
	//Object.disabled=true;
	while (true){
		if (Object.tagName=="body") return;
		if (Object.className=="divPopupDialog"){break;}
		Object = Object.parentNode;
	}
	//alert (Object.id);
	var error = ""
	var ajax = new Ajax(_SYSTEM);
	ajax.add("type","logout");
	ajax.Sincrono=true
	ajax.Funcion = loadMenu;
	//var ids = loading(document.getElementById('cargar'));
	var ids = loading(Object);
	ajax.anexo = ids;
	ajax.anexo1 = Object.id;
	ajax.execute();
	clearTabs();
}